package com.anrongtec.zcpt.manager;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.module.AppModel;
import com.anrongtec.zcpt.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;
import com.art.base.util.ApkUtils;
import com.art.base.util.FileUtils;
import com.art.base.util.TimeUtil;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;

/**
 * 安装记录adapter
 * 
 * @author dongtianhao
 *
 */
public class AppInstallRecordAdapter<T> extends CommonAdapter<AppModel>
		implements StickyRecyclerHeadersAdapter<ViewHolder> {

	public AppInstallRecordAdapter(Context context, int layoutId,
			List<AppModel> datas) {
		super(context, layoutId, datas);
	}

	@Override
	protected void convert(ViewHolder holder, final AppModel info, int position) {
		holder.setImageDrawable(R.id.iv_item_app_icon, info.getLocalIcon());
		holder.setText(R.id.tv_item_app_name, info.getAppName());
		
//		holder.setStar(R.id.ratingbar_item_app_score, info.getScore() == 0 ? 3.5f
//				: info.getScore());
		holder.setVisible(R.id.ratingbar_item_app_score, false);
		
		holder.setText(R.id.tv_item_app_version_name,
				info.getLocalVersionName());
		holder.setText(
				R.id.tv_item_app_total_size,
				FileUtils.formatFileSize(mContext.getApplicationContext(),
						info.getSize()));
		holder.setText(R.id.btn_item_app_install, "打开");
		holder.setOnClickListener(R.id.btn_item_app_install,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
					/*	ApkUtils.openAPP(mContext.getApplicationContext(),
								info.getAppPackName());*/
						if (info.getActivity() == null||info.getActivity().equals("")) {//有图标的打开方式
							ApkUtils.openAPP(mContext.getApplicationContext(),
									info.getAppPackName());
						} else {//无图标打开方式
							ApkUtils.openAPP(mContext.getApplicationContext(),
									info.getAppPackName(), info.getActivity());
						}
					}
				});
	}

	@Override
	public long getHeaderId(int position) {
		long id = TimeUtil.parseDataFromLong(
				mDatas.get(position).getFirstInstallTime()).hashCode();
		id = Math.abs(id);
		return id;
	}

	@Override
	public ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
		ViewHolder holder = ViewHolder.createViewHolder(mContext, parent,
				R.layout.item_header_down_manager);
		return holder;
	}

	@Override
	public void onBindHeaderViewHolder(ViewHolder holder, int position) {
		String header = TimeUtil.parseDataFromLongUTF(mDatas.get(position)
				.getFirstInstallTime());
		holder.setText(R.id.tv_item_header_down_manager_name, header);
	}

}
